package com.litchi.tutor.controller;


import com.litchi.tutor.common.base.ResponseVO;
import com.litchi.tutor.common.entity.Recruit;
import com.litchi.tutor.common.util.HideSensitiveUtil;
import com.litchi.tutor.common.vo.RecruitResp;
import com.litchi.tutor.service.RecruitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyouzi
 * @since 2021-11-10
 */
@RestController
@RequestMapping("/recruit")
@Api("招募订单管理")
public class RecruitController {

    @Autowired
    RecruitService recruitService;

    @RequestMapping(value = "/recruit", method = RequestMethod.GET)
    @ApiOperation("获取所有招募")
    public ResponseVO getRecruit(@RequestParam(required = false, defaultValue = "1") Long current,
                                 @RequestParam(required = false, defaultValue = "10") Long limit,
                                 @RequestParam(required = false) String area,
                                 @RequestParam(required = false) String teachingSubjectIds,
                                 @RequestParam(required = false) String other,
                                 @RequestParam(required = false) String gradeId,
                                 HttpServletRequest request) {
        List<RecruitResp> list = recruitService.getRecruit(current,limit,area, teachingSubjectIds, other, gradeId, request);
        return ResponseVO.ok(list);
    }

    @RequestMapping(value = "/recruit/{id}", method = RequestMethod.GET)
    @ApiOperation("招募详情")
    public ResponseVO getRecruitById(@PathVariable Long id) {
        Recruit recruit = new Recruit();
        Recruit byId = recruitService.getById(id);
        BeanUtils.copyProperties(byId, recruit);
        String mobile = HideSensitiveUtil.protectedMobile(recruit.getMobile());
        recruit.setMobile(mobile);
        return ResponseVO.ok(recruit);
    }

    @RequestMapping(value = "/recruit", method = RequestMethod.POST)
    @ApiOperation("发布招募订单")
    public ResponseVO createRecruit(@RequestBody Recruit recruit, String title, Map<String, String> map, HttpServletRequest request) {
        recruitService.createRecruit(recruit, title, map, request);
        return ResponseVO.ok(null);
    }

    @RequestMapping(value = "/recruit", method = RequestMethod.PUT)
    @ApiOperation("修改招募订单")
    public ResponseVO updateRecruitById(@RequestBody Recruit recruit) {
        if (recruit.getId() == null) {
            return ResponseVO.error("请传入id");
        }
        else {
            recruitService.updateById(recruit);
            return ResponseVO.ok(null);
        }
    }

    @RequestMapping(value = "/recruit/{id}", method = RequestMethod.DELETE)
    @ApiOperation("删除招募订单")
    public ResponseVO deleteRecruit(@PathVariable Long id) {
        recruitService.removeById(id);
        return ResponseVO.ok(null);
    }

}

